package tech.yunland.shop.web;

import com.moyuer.bsf.common.Pagination;
import com.moyuer.bsf.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tech.yunland.shop.domain.model.GoodsStock;
import tech.yunland.shop.domain.service.GoodsStockService;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 商品倉庫控制器
 *
 * @author 文卡<wkwenka@gmail.com>  on 17-3-27.
 */
@RestController
@RequestMapping("/goodsStock")
public class GoodsStockController extends BaseController {

    @Autowired
    private GoodsStockService goodsStockService;


    /**
     * 根據條件獲取倉庫列表
     *
     * @param goodsIds
     * @param organId
     * @param states
     * @param param
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Pagination<GoodsStock> getList(@RequestParam(required = false) List<String> goodsIds,
                                          @RequestParam(required = false) String organId,
                                          @RequestParam(required = false) List<Integer> states,
                                          @RequestParam(required = false) String param,
                                          @RequestParam(required = false) Integer page,
                                          @RequestParam(required = false) Integer rows) {

        if (organId == null) {
            organId = this.primaryOrganId;
        }

        Pagination<GoodsStock> pagination = new Pagination<>(page, rows);
        Integer startIdx = pagination.getStartIdx();
        pagination.setRecords(goodsStockService.getStoreList(param, goodsIds, organId, states, startIdx, rows));
        pagination.setCount(goodsStockService.getStoreListSize(param, goodsIds, organId, states));
        return pagination;
    }

    @RequestMapping(value = "/user/list", method = RequestMethod.GET)
    public Pagination getUseStockList(@RequestParam(required = false) List<String> goodsIds,
                                      @RequestParam(required = false) String organId,
                                      @RequestParam(required = false) String param,
                                      @RequestParam(required = false) Integer page,
                                      @RequestParam(required = false) Integer rows) {
        if (organId == null) {
            organId = this.primaryOrganId;
        }

        Pagination pagination = new Pagination<>(page, rows);
        Integer startIdx = pagination.getStartIdx();
        List<Map<String, Object>> useStock = goodsStockService.getUseStock(goodsIds, organId, param, startIdx, rows);

        List<Map<String, Object>> canBeUserdStocks = new LinkedList<>();
        for (Map<String, Object> args : useStock) {
            HashMap<String, Object> newArgs = new HashMap<>();
            newArgs.put("goodsName", args.get("name"));
            newArgs.put("storeNum", args.get("sum"));
            newArgs.put("id", args.get("goods_id"));
            newArgs.put("state", "可用");
            canBeUserdStocks.add(newArgs);
        }

        pagination.setRecords(canBeUserdStocks);
        pagination.setCount(goodsStockService.getUseStockSize(goodsIds, organId, param));
        return pagination;
    }

    /**
     * 获取日报表
     * @param page
     * @param rows
     * @param date 指定日期  格式 yyyy-mm-dd
     * @return
     */
    @RequestMapping(value = "/day-report")
    public Pagination<Map<String,Object>> getDayReport(@RequestParam(required = false) Integer page,
                                                       @RequestParam(required = false) Integer rows,
                                                       @RequestParam String date,
                                                       @RequestParam(required = false) String param){

        Pagination pagination = new Pagination<>(page, rows);
        Integer startIdx = pagination.getStartIdx();
        int totalCount = goodsStockService.getDayReportsGoodStoreCount(param,date,this.organIds).intValue();
        List<Map<String,Object>> records = goodsStockService.getDayReportGoodStore(param,date,organIds,startIdx,rows);
        pagination.setCount(totalCount);
        pagination.setRecords(records);
        return pagination;
    }

    /**
     * 获取月报表
     * @param page
     * @param rows
     * @param date 指定日期  格式 yyyy-mm
     * @return
     */
    @RequestMapping(value = "/month-report")
    public Pagination<Map<String,Object>> getMonthReport(@RequestParam(required = false) Integer page,
                                                         @RequestParam(required = false) Integer rows,
                                                         @RequestParam String date,
                                                         @RequestParam(required = false) String param){

        Pagination pagination = new Pagination<>(page, rows);
        Integer startIdx = pagination.getStartIdx();
        int totalCount = goodsStockService.getMonthReportsGoodStoreCount(param,date,this.organIds).intValue();
        List<Map<String,Object>> records = goodsStockService.getMonthReportGoodStore(param,date,organIds,startIdx,rows);
        pagination.setCount(totalCount);
        pagination.setRecords(records);
        return pagination;
    }

    /**
     * 获取汇总报表
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/all-report")
    public Pagination<Map<String,Object>> getAllReport(@RequestParam(required = false) Integer page,
                                                       @RequestParam(required = false) Integer rows,
                                                       @RequestParam(required = false) String param
                                                         ){

        Pagination pagination = new Pagination<>(page, rows);
        Integer startIdx = pagination.getStartIdx();
        int totalCount = goodsStockService.getAllReportsGoodStoreCount(param,this.organIds).intValue();
        List<Map<String,Object>> records = goodsStockService.getAllReportGoodStore(param,organIds,startIdx,rows);
        pagination.setCount(totalCount);
        pagination.setRecords(records);
        return pagination;
    }
}
